#pragma once

#include "UniBuffer.h"

class CEditBox
{
	friend class CIMEDevice;
public:
	CEditBox(void);
	~CEditBox(void);

	bool Create(HWND hWnd, LPDIRECT3DDEVICE9 device, const RECT& rcPanel, const char* fontface);

	void Update(float fDeltaTime);
	void Render();

	bool MsgProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );


	inline const RECT& GetControlRect() { return m_rcPanel; }

protected:
	void RenderControl();
	void RenderText();
	void RenderCaret();
	void RenderSelectedRegion();

	void InsertChar(const int nCP, const WCHAR wChar);			// nCP : Caret Position, wChar : one wide character
	void InsertChar(const int nCP, const std::wstring wtext);	// nCP : Caret Position, wtext : more wide character
	void RemoveChar(const int nCP, bool bPrior = true);			// nCP : Caret Position, bPrior : former or latter from Caret Position.

	void PlaceCaret(const int nCP );							// nCP : Caret Position
	int GetCaretPos() const { return m_nCaretPos; }
	void ResetBlankCaret() { m_fBlankCaretTime = 0; }

	void SetSelectedStart(const int nStartPos) { m_nSelStart = nStartPos; }
	int GetSelectedStart() const { return m_nSelStart; }

	int GetTextLength() { return m_kTextBuffer.GetTextSize(); }

	int GetStringHeight() { return 16; }
	int	GetStringWidth(int nStartPos = 0, int nCaretPos = -1 );
	int GetWidthFromCharPosition(const int nX );

	void CopyToClipboard();
	void PasteFromClipboard();

private:
	CIMEDevice* m_pIME;
	ID3DXFont* m_pFont;
	ID3DXLine* m_pCaretLine;

	CUniBuffer m_kTextBuffer;
	CUniBuffer m_kCompositBuffer;

	RECT m_rcPanel;
	RECT m_rcText;

	float m_fBlankCaretTime;

	int m_nFirstVisible;
	int m_nSelStart;
	int m_nCaretPos;

	bool m_bFlipCaret;
};
